package com.dingding.javabasic.Juc.可见性问题;

/**
 * @author liugongding
 * @description: 可见性问题
 * @date 2022/8/23 9:14 PM
 */
public class Visibility {


    private  static volatile boolean flag = true;

    /**
     * 线程T2对共享变量flag的修改，线程T2并没有并没有立即得到修改的最新值
     */
    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            while (flag) {
                System.out.println("线程"+Thread.currentThread().getName()+"不断读取共享变量");
            }
        },"T1").start();

        Thread.sleep(1000);
        new Thread(() -> {
            flag = false;
            System.out.println("线程"+Thread.currentThread().getName()+"修改了flag为false");
        },"T2").start();
    }
}
